package com.kiwi.cases.mario;

import org.apache.log4j.Logger;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;

import java.lang.reflect.Type;

public class MyStompSessionHandler extends StompSessionHandlerAdapter {
    private Logger logger = Logger.getLogger(MyStompSessionHandler.class);
    public MyStompSessionHandler(String roomId,String myUid,String opponentUid)
    {
        this.roomId = roomId;
        this.myUid = myUid;
        this.opponentUid= opponentUid;
    }
    private String roomId;
    private String myUid;
    private String opponentUid;
    @Override
    public void afterConnected(StompSession session, StompHeaders connectedHeaders)
    {
        logger.info("New session: {}"+session.getSessionId());
        session.subscribe("/topic/room/" + roomId, this);
        String loadingMsg="{\"code\":1200, \"content\":{\"uid\":"+myUid+", \"percent\":100}}";
        logger.info("进度加载消息："+loadingMsg);
        session.send("/app/room/" + roomId, loadingMsg.getBytes());
        for(int i=0;i<10;i++) {
            String walkPathMsg = "{\"code\":1400, \"content\":{\"uid\":" + myUid + ", \"dirction\":0, \"level\":2, \"right\":true}}";
            logger.info("运动轨迹消息：" + walkPathMsg);
            session.send("/app/room/" + roomId, walkPathMsg.getBytes());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
        exception.printStackTrace();
    }

    @Override
    public Type getPayloadType(StompHeaders headers) {
        return Message.class;
    }

    @Override
    public void handleFrame(StompHeaders headers, Object payload) {
        logger.info("Received: {}"+((Message) payload));
    }


}
